package njci.softerware.com.myexpress2017.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class WeekToDay {

    public static void main(String[] args) {
        List list = getWeekbeginAndEnd(-1);
        Iterator it = list.iterator();
        while(it.hasNext()){
           System.out.println(it.next());
        }
    }

    @SuppressWarnings({"deprecation", "unchecked"})
    public static List<String> dateToWeek(Date mdate, String strDate) {
        if (strDate != null && strDate.length() > 0) {
            try {
                mdate = new SimpleDateFormat("yyyy-MM-dd").parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<String> useList = new ArrayList<String>();
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 0; a < 8; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a, fdate);
        }
        String beginweek = new SimpleDateFormat("yy-MM-dd").format(list.get(0));
        String endweek = new SimpleDateFormat("yy-MM-dd").format(list.get(6));
        useList.add(beginweek);
        useList.add(endweek);
        return useList;
    }

    public static List<String> getWeekbeginAndEnd(int n) {
        List<String> list = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        // n为推迟的周数，1下周周，-1上周，2下周，依次类推
        String monday;
        cal.add(Calendar.DATE, n * 7);
        // 想周几，这里就传几Calendar.MONDAY（TUESDAY...）
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        monday = new SimpleDateFormat("yy-MM-dd").format(cal.getTime());
        list.add(monday);

        for (int i = 1; i < 7; i++) {
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, n * 7);
            // 想周几，这里就传几Calendar.MONDAY（TUESDAY...）
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            cal.add(Calendar.DAY_OF_MONTH, +i);
            String weekDay = new SimpleDateFormat("yy-MM-dd").format(cal.getTime());
            list.add(weekDay);
        }
        return list;
    }

    public static String getMothCompareKnow(int n) {
        Calendar calendar = Calendar.getInstance();
        // 获得当前时间的月份，月份从0开始所以结果要加1
        int month = calendar.get(Calendar.MONTH) + 1 + n;
        int year = calendar.get(Calendar.YEAR);
        String tmp = year + "";
        tmp = tmp.substring(2, tmp.length());
        String mTmp = month + "";
        if (mTmp.length() == 1) {
            mTmp = "0" + mTmp;
        }
        return tmp + "-" + mTmp;
    }


    public static String getKnowDay(){
        SimpleDateFormat formate = new SimpleDateFormat("yy-MM-dd");
        String know = formate.format(new Date());
        return  know;
    }
}
